<template>
  <div class="app-container">
    <!-- 列表 -->
    <el-card shadow="never" class="table-container">
      <template
        #header
        v-if="authAllPerm(['sys:org:menu:add', 'sys:org:menu:del'])"
      >
        <el-button
          type="success"
          plain
          @click="openDialog()"
          v-hasPerm="['sys:org:menu:add']"
        >
          <i-ep-plus />
          新增
        </el-button>
        <el-button
          type="danger"
          plain
          :disabled="ids.length === 0"
          :loading="deleteLoading"
          @click="handleTableDelete()"
          v-hasPerm="['sys:org:menu:del']"
        >
          <i-ep-delete />
          删除
        </el-button>
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
        row-key="id"
        :indent="30"
        default-expand-all
        :tree-props="{
          children: 'nodes',
          hasChildren: 'hasChildren',
        }"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="组织机构名称"
          prop="organizationName"
          minWidth="250"
          align="left"
          show-overflow-tooltip
        />
        <el-table-column
          label="组织机构编码"
          prop="organizationCode"
          show-overflow-tooltip
        />
        <el-table-column
            label="负责人"
            prop="chargeManagerName"
            align="center"
            show-overflow-tooltip
        />
        <el-table-column
          label="组织机构类型"
          align="center"
          show-overflow-tooltip
        >
          <template #default="scope">
            {{ useDictLabelShow(organTypeList, scope.row.organizationType) }}
          </template>
        </el-table-column>
        <el-table-column label="是否国内机构" align="center" width="140">
          <template #default="scope">
            <el-tag v-if="scope.row.internal === 1">境内机构</el-tag>
            <el-tag v-else type="warning">境外机构</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="加入年份" prop="addYear" width="150" />

        <el-table-column
          fixed="right"
          label="操作"
          width="260"
          v-if="
            authAllPerm([
              'sys:org:menu:operate:add',
              'sys:org:menu:operate:edit',
              'sys:org:menu:del',
            ])
          "
        >
          <template #default="scope">
            <el-button
              type="success"
              size="small"
              link
              @click="openDialog(scope.row, 'child')"
              v-hasPerm="['sys:org:menu:operate:add']"
            >
              <i-ep-plus />
              添加子机构
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              v-hasPerm="['sys:org:menu:operate:edit']"
            >
              <i-ep-edit />
              编辑
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="handleTableDelete(scope.row.id)"
              v-hasPerm="['sys:org:menu:del']"
            >
              <i-ep-delete />
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 新增 -->
    <add-organization
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      :parentId="addDialog.parentId"
      @reset-table="reset"
      @change-visible="changeVisible"
      @get-type-list="getTypeList"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable } from "@/utils/useTable";
import { useDictLabelShow } from "@/utils/commonHooks";
import request from "@/utils/request";
import { authAllPerm } from "@/utils/index";

defineOptions({
  name: "SYSTEMROLE",
  inheritAttrs: false,
});

// 是否是境内机构筛选
function filterTag(value: string, row: any) {
  return row.internal === value;
}

// 请求参数
const requestApi = "/system/sysOrganization/getTree";

// 是否需要分页
const pagination = false;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  organizationName: "",
};

const deleteParam = {
  api: "/system/sysOrganization/delete",
  data: {},
};

// 如果初始化表格需要排序=》const initParam = reactive({sort:''});
// 监听页面 initParam 改化，重新获取表格数据
// watch(() => initParam, getTableList, { deep: true });

// 数据请求错误后处理
function requestError() {}

// 数据请求错误后处理
// function dataCallBack() {}   //需要处理
const dataCallBack = ref(undefined); //无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  ids,
  loading,
  getTableList,
  reset,
  handleSelectionChange,
  handleTableDelete,
  deleteLoading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

// 获取组织机构类型列表
const organTypeList: any = ref([]);
function getTypeList(list: any) {
  organTypeList.value = list;
}

/** 打开角色表单弹窗 */
const addDialog = reactive<DialogOption>({
  visible: false,
  title: "",
  data: {},
  parentId: "",
});

async function getSystemOrgan(id: string) {
  const { data } = await request({
    url: `system/sysOrganization/get/${id}`,
    method: "post",
  });
  addDialog.data = data;
}

function openDialog(roleObj?: any, type?: any) {
  if (roleObj && !type) {
    // 编辑
    addDialog.title = `【${roleObj.organizationName}】编辑组织机构`;
    getSystemOrgan(roleObj.id);
    addDialog.parentId = "";
  } else if (roleObj && type) {
    // 添加子机构
    addDialog.title = `【${roleObj.organizationName}】添加子机构`;
    addDialog.parentId = roleObj.id;
    addDialog.data = {};
  } else {
    addDialog.data = {};
    addDialog.title = "新建组织机构";
  }
  addDialog.visible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
}
</script>
